import java.util.Scanner;

public class Vigenere {

    static String alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String dealK(String str,String K){
        K = K.toUpperCase();
        K = K.replaceAll("[^A-Z]", "");
        StringBuilder sb = new StringBuilder(K);
        String key = "";
        if(sb.length() != str.length()){
            if(sb.length() < str.length()){
                while(sb.length() < str.length()){
                    sb.append(K);
                }
            }
            key = sb.substring(0,str.length());
        }
        return key;
    }

    public static String encrypt(String P,String K){
        P = P.toUpperCase();
        P = P.replaceAll("[^A-Z]", "");
        K = dealK(P,K);
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i < K.length();i++){
            int row = alpha.indexOf(K.charAt(i));
            int col = alpha.indexOf(P.charAt(i));
            int index = (row+col) % 26;
            sb.append(alpha.charAt(index));
        }
        return sb.toString();
    }

    public static String decrypt(String C,String K){
        C = C.toUpperCase();
        C = C.replaceAll("[^A-Z]", "");
        K = dealK(C,K);
        int len = K.length();
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i < len;i++){
            int row = alpha.indexOf(K.charAt(i));
            int col = alpha.indexOf(C.charAt(i));
            int index;
            if(row > col){
                index = col + 26 - row;
            }else{
                index = col - row;
            }
            sb.append(alpha.charAt(index));
        }
        return sb.toString();
    }
    public static void menu() {
        System.out.println("1.明文加密");
        System.out.println("2.密文解密");
        System.out.println("3.退出");
        System.out.print("请选择菜单功能：");
    }
    public static void go(int choice) {
        boolean flag = false;
        do {
            if(choice == 1) {
                System.out.print("请输入明文：");
            }else {
                System.out.print("请输入密文：");
            }
            Scanner scanner = new Scanner(System.in);
            String P = scanner.nextLine();
            System.out.print("请输入密钥：");
            String K = scanner.nextLine();
            switch (choice) {
                case 1:
                    System.out.println("密文为：" + encrypt(P,K));
                    break;
                case 2:
                    System.out.println("明文为：" + decrypt(P,K));
                    break;
                default:
                    System.out.println("出错！");
                    break;
            }
        }while(flag);
    }
    public static void main(String[] args){
        int choice;
        int flag = 0;
        while(flag == 0) {
            menu();
            Scanner in = new Scanner(System.in);
            choice = in.nextInt();
            switch (choice) {
                case 1:
                    go(choice);
                    break;
                case 2:
                    go(choice);
                    break;
                case 3:
                    flag++;
                    break;
                default:
                    System.out.println("出错！");
                    break;
            }
        }
        System.out.println("谢谢您的使用！");
    }
}
